package cn.piggy.gateway.center.interfaces;

import cn.piggy.gateway.center.application.ILoadBalancingService;
import cn.piggy.gateway.center.domain.nginx.model.aggregates.NginxConfig;
import cn.piggy.gateway.center.domain.nginx.model.vo.LocationVO;
import cn.piggy.gateway.center.domain.nginx.model.vo.UpstreamVO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/wg/admin/load")
public class LoadBalancingManage {

    private Logger logger = LoggerFactory.getLogger(GatewayConfigManage.class);

    @Resource
    private ILoadBalancingService loadBalancingService;


    /**
     * http://localhost:8001/wg/admin/load/updateNginxConfig
     * {"upstreamList":[{"name":"11","strategy":"22","servers":["123","123"]}],
     * "locationList":[{"name":"123","proxyPass":"456"}]}
     */
    @PostMapping(value = "updateNginxConfig", produces = "application/json;charset=utf-8")
    public void updateNginxConfig(@RequestBody NginxConfig nginxConfig) {
        try {
            logger.info("刷新Nginx配置文件开始 nginxConfig：{}", JSON.toJSONString(nginxConfig));
            loadBalancingService.updateNginxConfig(nginxConfig);
            logger.info("刷新Nginx配置文件完成");
        } catch (Exception e) {
            logger.error("刷新Nginx配置文件失败", e);
        }
    }

}
